package com.xcw.utils;

import cn.hutool.core.date.DateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.*;

public class DateKit {

    public static final String YYYY_MM_DD = "yyyy-MM-dd";

    public static final String YYYYMM = "yyyyMM";

    public static final String YYYY_MM_DD_HH_mm_ss = "YYYY_MM_DD HH_mm_ss";

    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static final String YYYY_MM_DD_T_HH_MM_SS = "yyyy-MM-dd'T'HH:mm:ss";

    /**
     * @param lo 毫秒数
     * @return String yyyy-MM-dd HH:mm:ss
     * @Description: long类型转换成日期
     */
    public static String longToDate(long lo) {
        Date date = new Date(lo);
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sd.format(date);
    }

    /**
     * @param lo     毫秒数
     * @param patten 自定义时间格式
     * @return String
     * @Description: long类型转换成日期
     */
    public static String longToDate(long lo, String patten) {
        Date date = new Date(lo);
        SimpleDateFormat sd = new SimpleDateFormat(patten);

        TimeZone timeZone = TimeZone.getDefault();
        if (null != timeZone) {
            sd.setTimeZone(timeZone);
        }
        return sd.format(date);
    }

    /**
     * 获取精确到秒的时间戳
     *
     * @param date
     * @return
     */
    public static long getTimestamp(Date date) {
        if (null == date) {
            return 0;
        }
        return date.getTime() / 1000;
    }

    public static String getDateStrByNow() {
        return DateUtil.format(new Date(), YYYY_MM_DD);
    }

    public static String getDateSecondStrByNow() {
        return DateUtil.format(new Date(), YYYY_MM_DD_HH_MM_SS);
    }

    public static String getMonthStrCurrent() {
        return DateUtil.format(new Date(), YYYYMM);
    }

    /**
     * 获取精确到秒的时间戳
     *
     * @param date
     * @return
     */
    public static Long getStapTime(String date) {
        SimpleDateFormat sd = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        Date dates = null;
        try {
            dates = sd.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(dates);

        return todayStart.getTimeInMillis() / 1000L;
    }

    /**
     * yyyy-MM-dd HH:mm:ss
     * 根据输入时间来获取Date
     *
     * @param dateStr
     * @return
     */
    public static Date getDateByStr(String dateStr) {
        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        Date date = null;
        try {
            date = format.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String DateToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        String format = sdf.format(date);
        return format;
    }

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime   当前时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取前几日某日的最小时间的秒级时间戳
     *
     * @param past
     * @return
     */
    public static Long getPastDayMinTimeStamp(int past) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -past);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        return c.getTimeInMillis() / 1000;
    }

    /**
     * 获取前几日某日的最大时间的秒级时间戳
     *
     * @param past
     * @return
     */
    public static Long getPastDayMaxTimeStamp(int past) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -past);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        return c.getTimeInMillis() / 1000;
    }


    /**
     * 获取上一个月的今天，当31号时候，上个月没有今天的日期取的是这个月的1号的
     *
     * @return
     */
    public static String getLastMonthToday() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(YYYY_MM_DD);
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月
        date = calendar.getTime();
        return dateFormat.format(date);
    }

    /**
     * 获取某天最大时间
     *
     * @param date
     * @return
     */
    public static Date getEndOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        ;
        LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
        return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 获得某天最小时间00:00:00
     */
    public static Date getStartOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
        return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 当月第一天
     *
     * @return
     */
    public static String getFirstDayOfMonth() {
        // 获取当前年份、月份、日期
        Calendar cale = Calendar.getInstance();

        // 获取当月第一天和最后一天
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String firstday, lastday;
        // 获取前月的第一天
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        firstday = format.format(cale.getTime());
        return firstday;
    }


    /**
     * 当月第一天
     *
     * @return
     */
    public static String getFirstDayOfLastMonth() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //获取前月的第一天
        Calendar cal_1 = Calendar.getInstance();//获取当前日期
        cal_1.add(Calendar.MONTH, -1);
        cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        String firstDay = format.format(cal_1.getTime());
        return firstDay;
    }

    public static void main(String[] args) {
//        long currTime = getTimestamp(new Date());
//        System.out.println(currTime);
//        System.out.println(getMonthStrCurrent());
//        System.out.println(isEffectiveDate(new Date(), getDateByStr("2021-08-03 09:34:31"), getDateByStr("2021-08-04 08:34:31")));
//        System.out.println(getPastDayMinTimeStamp(1));
//        System.out.println(getPastDayMaxTimeStamp(1));
//        System.out.println(DateKit.DateToString(new Date()));
        Map param = new HashMap();

        param.put("a", "3333");

        System.out.println(param.get("a")); //3333

        if (param.containsKey("a")) {

            param.put("a", "565");
        }

        System.out.println(param.get("a"));//565


    }
}
